﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TLCApp.pulic;
using TLCApp.StkForm;

namespace TLCApp
{
    public partial class stk_ykd : Form
    {

        public stk_ykd(string pappno)
        {
            InitializeComponent();
            mappno = pappno;
        }
        private string mappno = "";
        private string _stype = "0"; //0新增 1修改, 2查看;  
       private BindingSource bsmx = new BindingSource();
   
        /// <summary>
        /// 传入值
        /// </summary>
        public string stype
        {
            get { return _stype; }
            set
            {
                if (value == null)
                {
                    return;
                }
                _stype = value;
            }

        }
        private string _rklx = "0";//出库类型：0 半成品出库：1配件出库 ，2发货出库
        public string rklx
        {
            get { return _rklx; }
            set
            {
                if (value == null)
                {
                    return;
                }
                _rklx = value;
            }

        }
        private string _kflx = "0";//-----0成品库, 1半成品 2,配件,3 材料
        public string kflx
        {
            get { return _kflx; }
            set
            {
                if (value == null)
                {
                    return;
                }
                _kflx = value;
            }

        }
        private bool _endable =true ;// 
        public bool mendable
        {
            get { return _endable; }
            set
            {
              
                _endable = value;
            }

        }
     

        private void txtBMMC_KeyDown(object sender, KeyEventArgs e)
        {

            if (e.KeyCode == Keys.Enter)
            {
                DataGridViewRow mSelectRow = new DataGridViewRow();
                DataTable dt = new DataTable();

                dt = Dal.stk.STK_YKD_STOCK_DATA("1").Tables[0];
                Frm_Query frmshow = new Frm_Query(dt, "");
                frmshow.filter = "代码|名称|拼音|五笔|类别";
                frmshow.strwhere = txtBMMC.Text.Trim();
                frmshow.chKht.Visible = false;
                frmshow.ShowDialog();
                mSelectRow = frmshow.mSelectRow;
                if (mSelectRow != null && mSelectRow.Index >= 0)
                {
                    txtBMDM.Text = mSelectRow.Cells["代码"].Value.ToString();
                    this.txtBMMC.Text = mSelectRow.Cells["名称"].Value.ToString();
                    txtgg.Text = mSelectRow.Cells["规格"].Value.ToString();
                    this.txtdw.Text = mSelectRow.Cells["单位"].Value.ToString();
                    if (kflx == "1")
                    {
                        cmbtype.SelectedValue = "2";
                    }
                    else
                    {
                        cmbtype.SelectedValue = (mSelectRow.Cells["新旧"].Value.ToString().ToUpper() == "新") ? "1" : "2";
                    }

                }
                else
                {

                    return;
                }


            }

            txtsl.Focus();


        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            this.bsmx.AddNew();
            txtBMMC.Focus();
        }

        private void stk_scrk_Load(object sender, EventArgs e)
        {



             setcontrol();
            DataTable dt= Dal.stk.Sys_GetStock(kflx).Tables[0];
             cmbkf.DataSource=dt ;
             cmbkf.DisplayMember = "stockmc";
             cmbkf.ValueMember = "stockid";
             cmbkf.SelectedIndex = 0;
             this.cmbks.DataSource = dt.Copy();
             cmbks.DisplayMember = "stockmc";
             cmbks.ValueMember = "stockid";
             cmbks.SelectedIndex = 1;
             cmbtype.DataSource = Cls_Func.GetBaseData("S01");
             cmbtype.DisplayMember = "DataName";
             cmbtype.ValueMember = "DataType";
             cmbtype.SelectedIndex = 0;
            querydata(mappno);
            bingdata();
        

        }
        void bingdata()
        {
            try
            {
                this.txtBMDM.DataBindings.Add("Text", bsmx, "xmdm");
                txtBMMC.DataBindings.Add("Text", bsmx, "xmmc");
                this.txtgg.DataBindings.Add("Text", bsmx, "matermodel");
                cmbtype.DataBindings.Add("Text", bsmx, "xmlbmc");
                cmbtype.DataBindings.Add("SelectedValue", bsmx, "xmlb");   
                this.txtsl.DataBindings.Add("Text", bsmx, "sl");
                txtbz.DataBindings.Add("Text", bsmx, "BZ");
                txtdw .DataBindings.Add("Text", bsmx, "unitname");
            }
            catch(Exception ex )
            {

                MessageBox.Show(ex.ToString());
            }
         
              
                
        }
        DataSet ds = new DataSet();
        void querydata( string appno)
        {
            try
            {
                
                    setbtn(mendable);

                    ds = Dal.stk.STK_YKD_STOCK_data(appno);
                ds.Tables[0].TableName = "STK_YKD";
                ds.Tables[1].TableName = "STK_YKDMX";
                DataTable dtzb = ds.Tables[0];
                bsmx.DataSource = ds.Tables[1];
                dgvmx.DataSource = bsmx;
                if (appno == "")
                {
                    DataRow dr = ds.Tables[0].NewRow();
                    //dr["CKLX"] = rklx;
                    dr["lrry"] = pulic.UserIfo.userId;
                    dr["lrrymc"] = pulic.UserIfo.userName;
                    dr["CZRQ"] = DateTime.Now.ToString("yyyy-MM-dd");
                    ds.Tables[0].Rows.Add(dr);
                }
                txtrkd.DataBindings.Add("Text", ds.Tables[0], "appno");
                //txthtbh.DataBindings.Add("Text", ds.Tables[0], "htbh");
                cmbkf.DataBindings.Add("SelectedValue", ds.Tables[0], "STOCKID_IN");
                cmbks.DataBindings.Add("SelectedValue", ds.Tables[0], "STOCKID_OUT");
                dtprkrq.DataBindings.Add("Value", ds.Tables[0], "CZRQ");
                txtrkry.DataBindings.Add("Text", ds.Tables[0], "LRRYMC");
                txtrkry.DataBindings.Add("Tag", ds.Tables[0], "LRRY");              
                txthtbz.DataBindings.Add("Text", ds.Tables[0], "bz");

            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.ToString());
            }
         
        }
        void setcontrol()
        {
            dgvmx.AutoGenerateColumns = false;
            this.dgvmx.AllowUserToAddRows = false;
            this.dgvmx.MultiSelect = false;
            this.dgvmx.ReadOnly = true;
            this.dgvmx.RowHeadersWidth = 10;
            this.dgvmx.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
        }

        private void dgvmx_Click(object sender, EventArgs e)
        {
            if (dgvmx.RowCount > 0)
            {

               
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (0 == this.bsmx.Count)
                return;
            if (!TLCApp.pulic.Cls_Popup.MsgYesNoBox("请确认是否修改！"))
            {
                return;
            }
            
            DataRowView dv = (DataRowView)bsmx.Current;

            dv.Delete();
          
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        void clearband()
        {

            txtrkd.DataBindings.Clear();
    
            cmbkf.DataBindings.Clear();
            cmbks.DataBindings.Clear();
            dtprkrq.DataBindings.Clear();
            txtrkry.DataBindings.Clear();
            txtrkry.DataBindings.Clear();
        
            txthtbz.DataBindings.Clear();

            ////////////////明细
            this.txtBMDM.DataBindings.Clear();
            txtBMMC.DataBindings.Clear();
            this.txtgg.DataBindings.Clear();
            cmbtype.DataBindings.Clear();
            cmbtype.DataBindings.Clear();
            this.txtsl.DataBindings.Clear();
            txtbz.DataBindings.Clear();
            txtdw.DataBindings.Clear();
        }
        private void button3_Click(object sender, EventArgs e)
        {
           
                 try
                     {
                         if (cmbkf.SelectedValue.ToString() == cmbks.SelectedValue.ToString())
                         {
                             TLCApp.pulic.Cls_Popup.MsgBox("移除库房不能和接收库房相同！");
                             return;
                         }
                         string strXml = TLCApp.pulic.XmlDataService.ToString(ds);
                         DataSet dsResult = Dal.stk.STK_YKD_STOCK_save(strXml);

                         if (dsResult.Tables[0].Rows[0]["rst"].ToString().ToUpper() == "T")
                         {
                             TLCApp.pulic.Cls_Popup.MsgBox("保存成功！");

                             clearband();
                             querydata(dsResult.Tables[0].Rows[0]["appno"].ToString());
                             bingdata();
                         }
                         else
                         {
                             TLCApp.pulic.Cls_Popup.MsgBox(dsResult.Tables[0].Rows[0]["msg"].ToString());
                         }
                     }
                     catch (Exception ex)
                     {

                         TLCApp.pulic.Cls_Popup.MsgErrBox(ex.ToString());
                     }
        }

        private void txtsl_Leave(object sender, EventArgs e)
        {
            if (txtBMDM.Text.Length > 0)
            {
                if (pulic.Cls_Func.CheckString(txtsl.Text.Trim()) != 5)
                {
                    TLCApp.pulic.Cls_Popup.MsgErrBox("数量必须输入正整数");
                    txtsl.Focus();
                }
           
            }
        }

        private void txthtbh_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
               
            }
        }

        private void txtbgry_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                //DataGridViewRow mSelectRow = new DataGridViewRow();
                //DataTable dt = new DataTable();
                //dt = Dal.stk.Sys_GetRYXX().Tables[0];
                //Frm_Query frmshow = new Frm_Query(dt, "");
                //frmshow.filter = "代码|名称|拼音|五笔|部门";
                //frmshow.strwhere = txtbgry.Text.Trim();
                //frmshow.chKht.Visible = false;
                //frmshow.chKht.Checked = false;
                //frmshow.ShowDialog();
                //mSelectRow = frmshow.mSelectRow;
                //if (mSelectRow != null && mSelectRow.Index >= 0)
                //{
                //    this.txtbgry.Tag = mSelectRow.Cells["代码"].Value.ToString();
                //    this.txtbgry.Text = mSelectRow.Cells["名称"].Value.ToString();
                //    this.btnAdd.Focus();
                //}
                //else
                //{

                //    return;
                //}
            }
        }

        private void txtrkry_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                DataGridViewRow mSelectRow = new DataGridViewRow();
                DataTable dt = new DataTable();
                dt = Dal.stk.Sys_GetRYXX().Tables[0];
                Frm_Query frmshow = new Frm_Query(dt, "");
                frmshow.filter = "代码|名称|拼音|五笔|部门";
                frmshow.strwhere = txtrkry.Text.Trim();
                frmshow.chKht.Visible = false;
                frmshow.chKht.Checked = false;
                frmshow.ShowDialog();
                mSelectRow = frmshow.mSelectRow;
                if (mSelectRow != null && mSelectRow.Index >= 0)
                {
                    this.txtrkry.Tag = mSelectRow.Cells["代码"].Value.ToString();
                    this.txtrkry.Text = mSelectRow.Cells["名称"].Value.ToString();
                 
                }
                else
                {

                    return;
                }
            }
        }

        private void txtysry_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
            //    DataGridViewRow mSelectRow = new DataGridViewRow();
            //    DataTable dt = new DataTable();
            //    dt = Dal.stk.Sys_GetRYXX().Tables[0];
            //    Frm_Query frmshow = new Frm_Query(dt, "");
            //    frmshow.filter = "代码|名称|拼音|五笔|部门";
            //    frmshow.strwhere = txtysry.Text.Trim();
            //    frmshow.chKht.Visible = false;
            //    frmshow.chKht.Checked = false;
            //    frmshow.ShowDialog();
            //    mSelectRow = frmshow.mSelectRow;
            //    if (mSelectRow != null && mSelectRow.Index >= 0)
            //    {
            //        this.txtysry.Tag = mSelectRow.Cells["代码"].Value.ToString();
            //        this.txtysry.Text = mSelectRow.Cells["名称"].Value.ToString();
            //         this.txtbgry.Focus();
            //    }
            //    else
            //    {

            //        return;
            //    }
            }
        }

        private void txtbgry_Leave(object sender, EventArgs e)
        {
          
            if (txtrkry.Text.Trim().Length == 0)
            {
                txtrkry.Tag = "";
            }
        }

      

        private void txtsl_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                 this.txtbz .Focus();
            }
        }

        private void txtbz_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.btnAdd.Focus();
            }
        }

        private void btnprint_Click(object sender, EventArgs e)
        {
            try
            {
                DataSet dsrept = Dal.stk.STK_YKD_PRINT(txtrkd.Text.Trim());

                //dsrept.Tables[0].TableName = "STK_RKD";
                //dsrept.Tables[1].TableName = "STK_RKDMX";
                string strpath = Application.StartupPath + "\\Report\\移库单.rpt";
                List<string> lparam = new List<string>();
                lparam.Add("rptname|" + pulic.UserIfo.rptname);
                lparam.Add("rptname1|移库单");
                PrintForm frmprint = new PrintForm(dsrept, strpath, lparam);
                frmprint.ShowDialog();
            }
            catch (Exception ex)
            {
                pulic.Cls_Popup.MsgErrBox(ex.ToString());
            }

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="flag"> true 可以 false</param>
        void setbtn( bool flag )
        {
            btnAdd.Enabled = flag;
            btnDelete.Enabled = flag;
            btnSave.Enabled = flag;
        
        }

        private void label2_Click(object sender, EventArgs e)
        {

        }
      
    }
}
